package org.dtrd.modules.evaluation.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.modules.evaluation.entity.bean.HappyInfo;
import org.dtrd.modules.evaluation.entity.po.DtrdEntEvaluationRdHappy;
import org.dtrd.modules.evaluation.mapper.DtrdEntEvaluationRdHappyMapper;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdHappyService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.StringJoiner;

/**
 * <p>
 * 总体幸福感评估 服务实现类
 * </p>
 *
 * @author QR
 * @since 2021-09-26
 */
@Service
@DS("multi-datasource1")
public class DtrdEntEvaluationRdHappyServiceImpl extends ServiceImpl<DtrdEntEvaluationRdHappyMapper, DtrdEntEvaluationRdHappy> implements IDtrdEntEvaluationRdHappyService {

    @Override
    public HappyInfo saveHappyInfo(HappyInfo happyInfo) {
        List<Integer> answerArr = happyInfo.getInput();
        int totalScore = 0;
        int tmpScore = 0;
        int a_interest = 0;// 对生活的满足和兴趣
        int b_healthConcern = 0;// 对健康的担心
        int c_energy = 0;// 精力
        int d_melancholy = 0;// 忧郁或愉快的心境
        int e_emotion = 0;// 对情感和行为的控制
        int f_nervous = 0;// 松弛和紧张
        StringJoiner joiner = new StringJoiner(",");
        for (int i = 0; i < answerArr.size(); i++) {
            tmpScore = answerArr.get(i);
            joiner.add(tmpScore + "");
            if (6 == i + 1) {
                a_interest += (5 - tmpScore);
            }
            if (11 == i + 1) {
                a_interest += tmpScore + 1;
            }
            if (10 == i + 1) {
                b_healthConcern += tmpScore + 1;
            }
            if (15 == i + 1) {
                b_healthConcern += (10 - tmpScore);
            }
            if (1 == i + 1 || 9 == i + 1) {
                c_energy += (6 - tmpScore);
            }
            if (14 == i + 1) {
                c_energy += tmpScore + 1;
            }
            if (17 == i + 1) {
                c_energy += tmpScore;
            }
            if (4 == i + 1 || 12 == i + 1) {
                d_melancholy += tmpScore + 1;
            }
            if (18 == i + 1) {
                d_melancholy += tmpScore;
            }
            if (3 == i + 1 || 13 == i + 1) {
                e_emotion += (6 - tmpScore);
            }
            if (7 == i + 1) {
                e_emotion += (5 - tmpScore);
            }
            if (2 == i + 1 || 5 == i + 1 || 8 == i + 1) {
                f_nervous += tmpScore + 1;
            }
            if (16 == i + 1) {
                f_nervous += (10 - tmpScore);
            }
        }
        totalScore = a_interest + b_healthConcern + c_energy + d_melancholy + e_emotion + f_nervous;
        String level = "";
        String output = "";
        if (totalScore <= 72) {
            level = "总体幸福感较低";
        } else if (totalScore <= 90) {
            level = "总体幸福感一般";
        } else if (totalScore <= 102) {
            level = "总体幸福感较高";
        } else if (totalScore <= 120) {
            level = "总体幸福感很高";
        }
        DtrdEntEvaluationRdHappy happy = new DtrdEntEvaluationRdHappy().init();
        happy.setInput(joiner.toString());
        happy.setTotal(totalScore);
        happy.setLevel(level);
        happy.setOutput(level);
        if (happyInfo.getPatientId() != null) {
            happy.setPatientId(happyInfo.getPatientId());
            save(happy);
        }
        return new HappyInfo().parseFromPo(happy);
    }

    @Override
    public HappyInfo getHappyInfoLatest(Integer patientId) {
        LambdaQueryWrapper<DtrdEntEvaluationRdHappy> queryWrapper = Wrappers.lambdaQuery(DtrdEntEvaluationRdHappy.class)
                .eq(DtrdEntEvaluationRdHappy::getPatientId, patientId)
                .orderByDesc(DtrdEntEvaluationRdHappy::getCreateTime)
                .last("limit 1");
        DtrdEntEvaluationRdHappy query = this.getOne(queryWrapper);
        HappyInfo happyInfo = new HappyInfo();
        if (query != null) {
            happyInfo = happyInfo.parseFromPo(query);
        }
        return happyInfo;
    }
}
